using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using BONSAI.Common;
using BONSAI.Common.DTO;

namespace BONSAI.DAL
{
	public class AboutUsDAO
	{
		public const string ABOUTUS_GET_BY_ID = "AboutUsGetByID";
		public const string ABOUTUS_GET_ALL = "AboutUsGetAll";
		public const string ABOUTUS_INSERT = "AboutUsInsert";
		public const string ABOUTUS_UPDATE_BY_ID = "AboutUsUpdateByID";
		public const string ABOUTUS_DELETE_BY_ID = "AboutUsDeleteByID";

		public const string ABOUTUSID = "AboutUsID";
		public const string NAMEFULL = "NameFull";
		public const string DESCRIPTION = "Description";
		public const string CREATEDATE = "Createdate";

		#region region properties
		public Database Database { get; set; }
		#endregion

		#region Constructor
		public AboutUsDAO()
		{
			Database = DatabaseFactory.CreateDatabase(BONSAIConstants.BONSAI_CONNECTION_STRING);
		}
		#endregion


		public AboutUsDTO GetByID(string id)
		{
			var command = Database.GetStoredProcCommand(ABOUTUS_GET_BY_ID);
			try
			{
				Database.AddInParameter(command, ABOUTUSID, DbType.String, id);
				var dataReader = Database.ExecuteReader(command);
				while (dataReader.Read())
				{
					var item = GetAboutUsFromDataReader(dataReader);
					return item;
				}
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public IList<AboutUsDTO> GetAll()
		{
			var command = Database.GetStoredProcCommand(ABOUTUS_GET_ALL);
			try
			{
				var dataReader = Database.ExecuteReader(command);
				var lstItem = new List<AboutUsDTO>();
				while (dataReader.Read())
				{
					var item = GetAboutUsFromDataReader(dataReader);
					lstItem.Add(item);
				}
				return lstItem;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public string Insert(AboutUsDTO aboutus)
		{
			var command = Database.GetStoredProcCommand(ABOUTUS_INSERT);
			try
			{
				Database.AddOutParameter(command, ABOUTUSID, DbType.String, 36);
				Database.AddInParameter(command, NAMEFULL, DbType.String, aboutus.NameFull);
				Database.AddInParameter(command, DESCRIPTION, DbType.String, aboutus.Description);
				Database.AddInParameter(command, CREATEDATE, DbType.String, aboutus.Createdate);
				var dataReader = Database.ExecuteReader(command);
				return Database.GetParameterValue(command, ABOUTUSID).ToString();
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public void UpdateByID(AboutUsDTO aboutus)
		{
			var command = Database.GetStoredProcCommand(ABOUTUS_UPDATE_BY_ID);
			try
			{
				Database.AddInParameter(command, ABOUTUSID, DbType.String, aboutus.AboutUsID);
				Database.AddInParameter(command, NAMEFULL, DbType.String, aboutus.NameFull);
				Database.AddInParameter(command, DESCRIPTION, DbType.String, aboutus.Description);
				Database.AddInParameter(command, CREATEDATE, DbType.String, aboutus.Createdate);
				var dataReader = Database.ExecuteReader(command);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}


		public void DeleteByID(string aboutusid)
		{
			var command = Database.GetStoredProcCommand(ABOUTUS_DELETE_BY_ID);
			try
			{
				Database.AddInParameter(command, ABOUTUSID, DbType.String, aboutusid);
				var dataReader = Database.ExecuteReader(command);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}


		private static AboutUsDTO GetAboutUsFromDataReader(IDataRecord dataReader)
		{
			var item = new AboutUsDTO();
			try
			{

				item.AboutUsID = (string.IsNullOrEmpty(Convert.ToString(dataReader[ABOUTUSID]))) ? "" : Convert.ToString(dataReader[ABOUTUSID]);
				item.NameFull = (string.IsNullOrEmpty(Convert.ToString(dataReader[NAMEFULL]))) ? "" : Convert.ToString(dataReader[NAMEFULL]);
				item.Description = (string.IsNullOrEmpty(Convert.ToString(dataReader[DESCRIPTION]))) ? "" : Convert.ToString(dataReader[DESCRIPTION]);
				item.Createdate = (string.IsNullOrEmpty(Convert.ToString(dataReader[CREATEDATE]))) ? DateTime.MinValue : DateTime.Parse(dataReader[CREATEDATE].ToString());
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return item;
		}
	}
}

